from rich.console import Console
from rich.panel import Panel
import os
from art import text2art
import sys_inf
import webbrowser
import tkinter as tk
from tkinter import messagebox
from rich.markdown import Markdown
import json
import re
from rich.console import Console
from rich.markdown import Markdown
from rich import pretty

def inf():
    model_name = "E_command"
    model_function = "命令行运行模块，可以调用本模块将print中的内容以命令行的形式输出！"
    return model_name, model_function

version, description, download_link = sys_inf.fetch_data_optimized()
console = Console()
def show_popup():
    # 创建一个隐藏的主窗口
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    # 弹出提示框
    messagebox.showinfo("版本更新", description)
    # 关闭主窗口
    root.quit()

def command_elysium(text):
    console.print(text, style="green")

def command_code(text):
    console.print(text, style="red")

def command(text):
    # 常规输出
    print(text)

def command_warn(text):
    # 警告输出
    data = "[bold red]警告：[/bold red]" + text
    warning_panel = Panel.fit(data)
    console.print(warning_panel)


def command_inf():
    # 程序信息展示部分，包括了Elysium的版本号以及更新地址等等信息
    os.system('cls' if os.name == 'nt' else 'clear')
    console.rule("[bold red] Elysium")
    ascii_art = text2art("Elysium", font='slant')
    # # slant smslant standard
    terminal_width = console.width
    art_lines = ascii_art.split("\n")
    max_art_width = max(len(line) for line in art_lines)
    padding_width = (terminal_width - max_art_width) // 2
    centered_art = "\n".join((" " * padding_width) + line for line in art_lines)
    console.print(centered_art)
    console.rule(f"[bold red]Beta {version}")
    style = 'bold white on black'
    console.print(f'Elysium designed by Leeguolon Version {version} \n寻找更多信息请访问 https://gitee.com/lee-guolon\n', style=style, justify='left')
    if version != "1.2":
        show_popup()
        webbrowser.open(download_link)
from rich.console import Console
from rich.markdown import Markdown

def show_markdown_from_input(markdown_text):
    console = Console()

    try:
        # 判断是否是 JSON 格式
        try:
            json_data = json.loads(markdown_text)
            # console.print("[bold green]JSON[/bold green]")
            # 使用 rich.pretty 来格式化打印 JSON
            console.print(pretty.Pretty(json_data))
            return
        except json.JSONDecodeError:
            pass  # 如果不是 JSON 格式，则继续检查其他格式

        # 判断是否是 HTML 格式
        if re.search(r'<[^>]+>', markdown_text):
            # console.print("[bold yellow]HTML[/bold yellow]")
            # 使用正则表达式去除 HTML 标签，保留文本内容
            text_content = re.sub(r'<[^>]*>', '', markdown_text)
            console.print(text_content)
            return

        # 判断是否是 Markdown 格式
        if markdown_text.startswith('#') or '```' in markdown_text:
            # console.print("[bold blue]Markdown[/bold blue]")
            markdown = Markdown(markdown_text)  # 渲染 Markdown 内容
            console.print(markdown)
            return

        # 默认处理为普通文本
        # console.print("[bold white]常规文本[/bold white]")
        console.print(markdown_text)

    except Exception as e:
        console.print(f"[bold red]处理内容时出错：{e}[/bold red]")